﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Yonkly.Controllers;
using Yonkly.Models;

namespace Yonkly.Models
{
    public class BaseViewData
    {
        public BaseViewData()
        {

        }
        public BaseViewData(BaseViewData mybase)
        {
            CurrentUser = mybase.CurrentUser;
            LoggedInUser = mybase.LoggedInUser;
            IsAuthenticated = mybase.IsAuthenticated;
            SearchTerm = mybase.SearchTerm;
        }

        public bool IsGroup { get; set; }

        public bool IsAuthenticated
        {
            get;
            set;
        }

        public string SearchTerm
        {
            get;
            set;
        }

        public UserItem LoggedInUser
        {
            get;
            set;
        }

        public UserItem CurrentUser
        {
            get;
            set;
        }


        private List<UserItem> sidebarFriends;
        public List<UserItem> SidebarFriends
        {
            get
            {
                if (sidebarFriends == null)
                {
                    if (LoggedInUser == null)
                        sidebarFriends = UserController.GetSidebarFriends(CurrentUser.UserId, Guid.Empty);
                    else
                        sidebarFriends = UserController.GetSidebarFriends(CurrentUser.UserId, LoggedInUser.UserId);
                }
                return sidebarFriends;
            }
        }

        private List<UserItem> sidebarFriendsOfFriends;
        public List<UserItem> SidebarFriendsOfFriends
        {
            get
            {
                if (CurrentUser == null || LoggedInUser == null)
                    return new List<UserItem>();
                if (sidebarFriendsOfFriends == null)
                {
                    sidebarFriendsOfFriends = UserController.GetSidebarFriendsOfFriends(LoggedInUser.UserId);
                }
                return sidebarFriendsOfFriends;
            }
        }


        private List<Group> topGroups;
        public List<Group> TopGroups
        {
            get
            {
                if(topGroups== null)
                {
                    using (var dc = new YonklyDataContext())
                    {
                        var groupIds = dc.GroupMembers.GroupBy(gm => gm.GroupId).OrderByDescending(o=> o.Count()).Take(10).Select(s=> s.Key);

                        var query = from g in dc.Groups
                                    where groupIds.Contains(g.Id)
                                    select g;
                        topGroups = query.ToList();
                    }
                }
                return topGroups;
            }
        }

        /// <summary>
        /// //todo Used only by one view for twitter friends - might need to be refactored
        /// out of the base view data
        /// </summary>
        public System.Web.Mvc.PagedList<TwitterFriendItem> TwitterFriends { get; set; }

    }
}